package com.itheima.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckGroupException;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * @author nglsk
 * @version v1.0
 * @date 2019/11/22 14:32
 * @description TODO
 **/

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Reference
    CheckGroupService checkGroupService;

    /**
     * 添加检查组
     *
     * @param checkGroup
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.add(checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return Result.success(MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    /**
     * 检查组分页显示
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 回显检查组数据
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById4Edit")
    public Result findById4Edit(Integer id) {
        try {
            Map map = checkGroupService.findById4Edit(id);
            return Result.success(MessageConstant.QUERY_CHECKGROUP_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 编辑检查组
     *
     * @param checkGroup
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup) {
        try {
            checkGroupService.edit(checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return Result.success(MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }


    /**
     * 删除检查组(查验关联关系)
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            checkGroupService.delete(id);
        } catch (CheckGroupException e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }


    /**
     * 删除检查组(不查验关联关系，直接删除)
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete1")
    public Result delete1(Integer id) {
        try {
            checkGroupService.delete1(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }



    /**
     * 查询所有的检查组
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        List<CheckGroup> checkGroupList = checkGroupService.findAll();

        if (CollectionUtil.isNotEmpty(checkGroupList)) {
            return Result.success(MessageConstant.QUERY_ALL_CHECKGROUP_SUCCESS, checkGroupList);
        }
        return Result.error(MessageConstant.QUERY_ALL_CHECKGROUP_FAIL);
    }
}

